package cj.web.sc.controllers;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.utils.ExportExcel;
import cj.web.admin.domain.UserInfo;
import cj.web.constants.MemuEnum;
import cj.web.constants.PayWayEnum;
import cj.web.constants.SCIsInvoiceEnum;
import cj.web.constants.SCOrderStatusEnum;
import cj.web.constants.SCShipperCodeEnum;
import cj.web.login.controllers.BaseController;
import cj.web.sc.dao.SCCrowdFundOrderDAO;
import cj.web.sc.domain.SCCrowdFundOrder;
import cj.web.sc.domain.SCOrder;
import cj.web.sc.domain.SCSeller;

@Path("/crowdFundOrder")
public class SCCrowdFundOrderController extends BaseController {

	private static Logger logger = Logger.getLogger(SCCrowdFundOrderController.class.getName());
	
	private final String[] headers = { "订单号", "商家名称", "参赛证号","订单详情", "订单总价", "收件人", "收件人电话", "收件人地址",
			"留言", "快递单号", "订单状态", "支付单号", "支付方式","支付时间" };
	
	@Autowired
	private SCCrowdFundOrderDAO fundOrderDao;
	
	@Get("show")
	public String show(Model model,SCCrowdFundOrder sCCrowdFundOrder) {
		model.add("PayWayEnum", EnumSet.allOf(PayWayEnum.class));
		model.add("InvoiceEnum", EnumSet.allOf(SCIsInvoiceEnum.class));
		model.add("StatusEnum", EnumSet.allOf(SCOrderStatusEnum.class));
		model.add("ShipperEnum", EnumSet.allOf(SCShipperCodeEnum.class));
		model.add("MemuEnum", MemuEnum.SCCrowdFundOrder);
		return "/views/sc/crowdFundOrderList.vm";
	}

	@Post("list")
	public String list(SCCrowdFundOrder sCCrowdFundOrder, HttpServletRequest request) {
		List<SCCrowdFundOrder> objs = fundOrderDao.query(sCCrowdFundOrder);
		int size = fundOrderDao.size(sCCrowdFundOrder);

		JSONObject jo = new JSONObject();
		jo.put("data", objs);
		jo.put("draw", sCCrowdFundOrder.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@" + jo.toString();
	}
	
	@Get("output")
	public void output(Model model, SCCrowdFundOrder order,
			@Param("gameName")String gameName,@Param("code")String code,@Param("goodsItemName")String goodsItemName,
			@Param("orderNo")String orderNo,@Param("receiverName")String receiverName,@Param("receiverPhone")String receiverPhone,
			@Param("memo")String memo,@Param("status")int status,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String sheetName = "众筹订单";
		String title = URLEncoder.encode(sheetName, "UTF-8"); // 进行转码，使其支持中文文件名
		response.setContentType("application/vnd.ms-excel"); // 生成提示信息
		response.setHeader("content-disposition", "attachment;filename="
				+ title + DateUtils.getNowTime(DateUtils.DATE_FULL_STR1)+ ".xls");
		OutputStream fOut = response.getOutputStream();

		List<Map> dataset = null;
		ExportExcel<SCOrder> ex = new ExportExcel<SCOrder>();

		
		String[] fields = { "orderNo", "sellerName", "code","itemDetails","discntPrice",
				"receiverName", "receiverPhone", "receiverAddress",
				"memo", "expressBill", "status", "wxTradeNo", "payWay","payAt" };

		order.setGameName(gameName);
		order.setCode(code);
		order.setGoodsItemName(goodsItemName);
		order.setOrderNo(orderNo);
		order.setReceiverName(receiverName);
		order.setReceiverPhone(receiverPhone);
		order.setMemo(memo);
		order.setStatus(status);
		dataset = fundOrderDao.output(order);

		for (Map map : dataset) {
			for (Object key : map.keySet()) {
				if(null == map.get(key)){
					map.put(key, "");
				}
			}
			String strStatus = map.get("status").toString();
			strStatus = "".equals(strStatus) ? "" : SCOrderStatusEnum.getName(Integer.parseInt(strStatus));
			map.put("status", strStatus);
			
			String strPayWay = map.get("payWay").toString();
			strPayWay = "".equals(strPayWay) ? "" : PayWayEnum.getName(Integer.parseInt(strPayWay));
			map.put("payWay", strPayWay);
		}
		ex.exportExcelWithStrCellStyle(sheetName, headers, fields, dataset, fOut,
				DateUtils.DATE_FULL_STR);
	}
	
	@Get("mutiModifyProgress")
	public String mutiModifyProgress(@Param("id")List<Integer> ids, @Param("progress")String progress){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		if(ids != null && ids.size()>0 && progress!=null && !"".equals(progress)){
			fundOrderDao.mutiUpdateProgress(ids, progress);
		}
		else{
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("createPage")
	public String createPage()
	{
		return "";
	}
	
	@Post("create")
	public String create(Model model,SCCrowdFundOrder sCCrowdFundOrder,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id)
	{
		return "";
	}
	
	@Post("update")
	public String update(Model model,SCCrowdFundOrder sCCrowdFundOrder,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("delete")
	public String delete(Model model,@Param("id") int id, HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		
		SCCrowdFundOrder order = fundOrderDao.queryById(id);
		if (order != null) {
			String acount = returnSessionObj(request).getAccount();
			order.setUpdateUser(acount);
			fundOrderDao.delete(order);
		}
		
		return "@"+jo.toString();
	}
}
